<template>
  <div class="update-gender">
    <van-picker
    show-toolbar
    title="选择性别"
    :default-index="value"
    :columns="columns"
    @cancel="$emit('close')"
    @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm(value, index) {
      // 增加loading状态
      this.$toast.loading({
        message: '更新中...',
        forbidClick: true,
        duration: 0
      })
      try {
        // 调用接口发送请求
        await updateUserProfile({
          gender: index
        })
        // 返回提示信息
        this.$toast.success('更新性别成功！')
        // 更改父组件值
        this.$emit('input', index)
        //  关闭弹层
        this.$emit('close')
      } catch (error) {
        this.$toast('更新性别失败！')
      }
    }
  }
}
</script>

<style scoped lang="less">
</style>
